trigger PlanDocStatusChange on Sales_Contract__c (after update) 
{
    List<Task> taskList = new List<Task>();
    List<Sales_Contract__Share> shareContractList = new List<Sales_Contract__Share>();

    String shareId = '';
    Date dt = null;
    Integer scSize = 0;
    for(Sales_Contract__c[] sc: [select Id, Name, (Select ActorId, IsPending from ProcessSteps order by createddate desc), FirstName__c, LastName__c, Status__c, RequiredReturnDate__c, Level1__c, Level2__c, Level3__c, Level4__c, Level5__c, Level6__c, Level7__c, Level8__c, Level9__c, Level10__c, Level11__c, Level12__c, Level13__c, Level14__c, Level15__c, Final_Approver__c, Final_Approver_Date__c, Level1_Due_Date__c, Level2_Due_Date__c, Level3_Due_Date__c, Level4_Due_Date__c, Level5_Due_Date__c, Level6_Due_Date__c, Level7_Due_Date__c, Level8_Due_Date__c, Level9_Due_Date__c, Level10_Due_Date__c, Level11_Due_Date__c, Level12_Due_Date__c, Level13_Due_Date__c, Level14_Due_Date__c, Level15_Due_Date__c, ownerId, CreatedById, UserID__c from Sales_Contract__c where Id in :Trigger.new and Status__c != 'Distributed'])
    {
        scSize = 0;
        for(Sales_Contract__c s: sc)
        {
            scSize++;
            if(s.Status__c.indexOf('Plan Rejected') != -1)
            {
                Task t = new Task();
                t.OwnerId = s.CreatedById;
                t.WhatId = s.Id;
                
                t.Subject = s.FirstName__c+' '+s.LastName__c+'\'s Plan Document has been Rejected';
                t.Description = s.FirstName__c+' '+s.LastName__c+'\'s Plan Document has been Rejected';
                
                t.Status = 'NotStarted';
                t.Type = 'Email';
                t.ActivityDate = Date.today();
                t.ReminderDateTime = DateTime.now();
                t.IsReminderSet = true;
                t.Priority = 'High';
                
                taskList.add(t);
            }
           
            if(s.Status__c.indexOf('Plan Approved') != -1)
            {
                shareId = '';
                
                for(ProcessInstanceHistory instanceHistory : s.ProcessSteps)
                {
                    if(instanceHistory.IsPending)
                    {
                        if(s.Level1__c == instanceHistory.ActorId)
                        {
                            if(s.Level2__c != null)
                            {
                                shareId = s.Level2__c;
                                dt = s.Level2_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level2__c == instanceHistory.ActorId)
                        {
                            if(s.Level3__c != null)
                            {
                                shareId = s.Level3__c;
                                dt = s.Level3_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level3__c == instanceHistory.ActorId)
                        {
                            if(s.Level4__c != null)
                            {
                                shareId = s.Level4__c;
                                dt = s.Level4_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level4__c == instanceHistory.ActorId)
                        {
                            if(s.Level5__c != null)
                            {
                                shareId = s.Level5__c;
                                dt = s.Level5_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level5__c == instanceHistory.ActorId)
                        {
                            if(s.Level6__c != null)
                            {
                                shareId = s.Level6__c;
                                dt = s.Level6_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level6__c == instanceHistory.ActorId)
                        {
                            if(s.Level7__c != null)
                            {
                                shareId = s.Level7__c;
                                dt = s.Level7_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level7__c == instanceHistory.ActorId)
                        {
                            if(s.Level8__c != null)
                            {
                                shareId = s.Level8__c;
                                dt = s.Level8_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level8__c == instanceHistory.ActorId)
                        {
                            if(s.Level9__c != null)
                            {
                                shareId = s.Level9__c;
                                dt = s.Level9_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level9__c == instanceHistory.ActorId)
                        {
                            if(s.Level10__c != null)
                            {
                                shareId = s.Level10__c;
                                dt = s.Level10_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level10__c == instanceHistory.ActorId)
                        {
                            if(s.Level11__c != null)
                            {
                                shareId = s.Level11__c;
                                dt = s.Level11_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level11__c == instanceHistory.ActorId)
                        {
                            if(s.Level12__c != null)
                            {
                                shareId = s.Level12__c;
                                dt = s.Level12_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level12__c == instanceHistory.ActorId)
                        {
                            if(s.Level13__c != null)
                            {
                                shareId = s.Level13__c;
                                dt = s.Level13_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level13__c == instanceHistory.ActorId)
                        {
                            if(s.Level14__c != null)
                            {
                                shareId = s.Level14__c;
                                dt = s.Level14_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level14__c == instanceHistory.ActorId)
                        {
                            if(s.Level15__c != null)
                            {
                                shareId = s.Level15__c;
                                dt = s.Level15_Due_Date__c;
                            }
                            else if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                        else if(s.Level15__c == instanceHistory.ActorId)
                        {
                            if(s.Final_Approver__c != null)
                            {
                                shareId = s.Final_Approver__c;
                                dt = s.Final_Approver_Date__c;
                            }
                        }
                    }
                    
                    break;
                }//end of for(ProcessInstanceHistory ....)

                Task t = new Task();
                t.OwnerId = shareId;
                t.WhatId = s.Id;
                
                t.Subject = s.FirstName__c+' '+s.LastName__c+'\'s Plan Document has been Approved';
                t.Description = s.FirstName__c+' '+s.LastName__c+'\'s Plan Document has been Approved.';
                
                t.Status = 'NotStarted';
                t.Type = 'Email';
                t.ActivityDate = dt;
                String remDateTime = dt+' 08:00:00';
                t.ReminderDateTime = Datetime.valueof(remDateTime) - 3;
                t.IsReminderSet = true;
                t.Priority = 'High';
                
                taskList.add(t);
                
                Sales_Contract__Share scShare  = new Sales_Contract__Share();
                  
                scShare.ParentId = s.Id;
                  
                // Set the ID of user or group being granted access.
                scShare.UserOrGroupId = shareId;                  

                scShare.AccessLevel = 'read';
                  
                // Set rowCause to 'manual' for manual sharing.
                // This line can be omitted as 'manual' is the default value for sharing objects.
                scShare.RowCause = Schema.Sales_Contract__Share.RowCause.Manual;
                shareContractList.add(scShare);                  
            }

            if(s.Status__c == 'Published' && s.UserID__c != UserInfo.getUserId())
            {
                Task t = new Task();
                t.OwnerId = s.UserID__c;
                t.WhatId = s.Id;
                t.Subject = 'Plan Document Published';
                t.Description = 'Plan Document Published.';
                t.Status = 'NotStarted';
                t.Type = 'Email';
                t.ActivityDate = s.RequiredReturnDate__c;
                String remDateTime = s.RequiredReturnDate__c+' 08:00:00';
                t.ReminderDateTime = Datetime.valueof(remDateTime) - 3;
                t.IsReminderSet = true;
                t.Priority = 'High';
                
                taskList.add(t);

                Sales_Contract__Share scShare  = new Sales_Contract__Share();
                  
                scShare.ParentId = s.Id;
                  
                  // Set the ID of user or group being granted access.
            
                scShare.UserOrGroupId = s.UserID__c;
                  
                  
                  // Set the access level.
                scShare.AccessLevel = 'read';
                  
                  // Set rowCause to 'manual' for manual sharing.
                  // This line can be omitted as 'manual' is the default value for sharing objects.
                scShare.RowCause = Schema.Sales_Contract__Share.RowCause.Manual;
                shareContractList.add(scShare);
            }
            
            Sales_Contract__History history = new Sales_Contract__History(ParentId = s.Id);
            if(sc.size() == scSize)
            {
                try
                {
                    insert taskList;
                    insert shareContractList;
                }
                catch(Exception e)
                {
                   
                }
                
                if(shareContractList.size() > 0)
                    shareContractList.clear();
                if(taskList.size() > 0)
                    taskList.clear();
            }
        }                                        
    }
}